﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using Gluten.Models;

namespace Gluten.Controllers
{
    [Authorize(Roles = "admin")]
    public class AdminController : Controller
    {

        private DBManager db = new DBManager();

        public ActionResult Index()
        {
            return View(db.GetLabel(Guid.Empty));
        }

        public ActionResult MenuManager()
        {

            ViewBag.Menu = db.GetAllMainMenu();
            return View();

        }

        public ActionResult MainManager()
        {
            return View();
        }

        public ActionResult CreateMenu()
        {
            return View();
        }

        [HttpPost]
        public void CreateMenu(Guid id, Menu menu)
        {
            menu.Parent = id;
            menu.Id = Guid.NewGuid();
            db.AddMenu(menu);
            if (id == Guid.Empty)
            {
                Response.Redirect(Url.Action("MenuManager"));
            }
            Response.Redirect(Url.Action("Menu") + "/" + id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public void Menu(SelectPages pagesRequest)
        {
            var pages = pagesRequest.GetOnlyChecked();
            if (pagesRequest.Action == "Delete")
            {
                db.DeletePages(pages);
            }
            else if (pagesRequest.Action == "Move")
            {
                db.MovePagesToMenu(pagesRequest.Destination, pages);
            }
            ViewBag.Menus = db.GetAllMainMenu();
            Response.Redirect(Url.Action("Menu"));
        }

        public ActionResult Menu(Guid id)
        {
            if (id == Guid.Empty)
            {
                Response.Redirect(Url.Action("MenuManager"));
            }
            ViewBag.Menus = db.GetAllMainMenu();
            ViewBag.Menu = db.GetMenu(id);
            ViewBag.Pages = db.GetAllPagesByMenu(id, true).ToList();
            ViewBag.Submenu = db.GetAllSubmenu(id);
            return View();
        }

        public ActionResult CreatePage(Guid id)
        {
            ViewBag.Menu = db.GetMenu(id);
            return View();
        }

        public ActionResult Page(Guid id)
        {
            ViewBag.Page = db.GetPage(id);
            return View(db.GetPage(id));
        }

        [HttpPost]
        public void Page(Guid id, Page page)
        {
            db.UpdatePage(page);
            Guid parentId = db.GetPage(id).MenuParent;
            Response.Redirect(Url.Action("Menu") + "/" + parentId);
        }

        [HttpPost]
        public void CreatePage(Guid id, Page page)
        {
            page.Id = Guid.NewGuid();
            page.MenuParent = id;
            db.AddPage(page);
            Response.Redirect(Url.Action("Menu") + "/" + id);
        }

        public void DeletePage(Guid id)
        {
            Guid parentId = db.GetPage(id).MenuParent;
            db.DeletePage(id);
            Response.Redirect(Url.Action("Menu") + "/" + parentId);

        }

        public void DeleteMenu(Guid id)
        {
            Guid parentId = db.GetMenu(id).Parent;
            db.DeleteMenu(id);
            if (parentId == Guid.Empty)
            {
                Response.Redirect(Url.Action("MenuManager"));
            }
            Response.Redirect(Url.Action("Menu") + "/" + parentId);
        }

        [HttpGet]
        public ActionResult EditMenu(Guid id)
        {
            ViewBag.Menu = db.GetMenu(id);
            return View();
        }

        [HttpGet]
        public ActionResult MenuHead(Guid id)
        {
            return View(db.GetMenu(id));
        }

        [HttpPost]
        public void EditMenu(Guid id, Menu menu)
        {
            db.UpdateMenu(menu);
            Response.Redirect(Url.Action("MenuManager"));
        }

        public ActionResult Label(Guid id)
        {
            var label = db.GetLabel(id);
            if (label == null)
            {
                label = new Label {Id = id};
                db.AddLabel(label);
                var f = db.GetLabel(id);
            }
            return View(label);
        }

        [HttpPost]
        public void Label(Label label)
        {
            db.UpdateLabel(label);
            Response.Redirect(Url.Action("Index"));
        }

        public ActionResult ChangeImage()
        {
            ViewBag.Error = "";
            return View();
        }

        [HttpPost]
        public ActionResult ChangeImage(Guid id, HttpPostedFile image)
        {
            var imageStartType = "image";
            if (Request.Files.Count > 0)
            {
                if (Request.Files[0].ContentType.StartsWith(imageStartType))
                {
                    var img = Request.Files.Get(0);
                    var dotSplittedName = img.FileName.Split('.');

                    var imagePath = "Content/imgs/label/" + Guid.NewGuid() + "." + dotSplittedName[dotSplittedName.Length - 1];
                    img.SaveAs(Request.PhysicalApplicationPath + imagePath);
                    db.LabelImageChange(id, imagePath);
                    Response.Redirect(Url.Content(Url.Action("Label") + "/" + id));
                }
            }
            ViewBag.Error = "Atata!!!";
            return View();
        }

        public ActionResult MainHead()
        {
            var menu = db.GetMenu(Guid.Empty);
            if (menu == null)
            {
                menu = new Menu { Id = Guid.Empty };
                db.AddMenu(menu);
            }
            return View(menu);
        }

        [HttpPost]
        public void MainHead(Menu menu)
        {
            db.UpdateMenu(menu);
            Response.Redirect(Url.Action("MainManager"));
        }

        [HttpPost]
        public void MenuHead(Menu menu)
        {
            db.UpdateMenu(menu);
            Response.Redirect(Url.Action("MenuManager"));
        }

    }
}